<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>业绩奖配置列表</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" media="all">
    <style>
        .productFxList-container {
            padding: 20px;
        }

        td[data-field="imgUrl"] div,
        td[data-field="negativeImgUrl"] div {
            height: 120px;
        }

        .lay-form {
            padding: 20px;
        }

        .productFxList-container {
            background-color: #fff;
        }

        .bar {
            display: flex;
            justify-content: flex-start;
            align-items: flex-end;
        }

        .searchInput {
            display: flex;
            align-items: flex-end;
        }

        .searchInput input {
            width: 200px;
        }

        .searchInput .layui-form-label {
            padding: 9px 0;
        }

        .bar .layui-form-label {
            padding: 0;
            padding-bottom: 4px;
            text-align: left;
        }

        .bar .layui-inline {
            margin-right: 20px;
        }

        .layui-table img {
            max-width: 200px;
            object-fit: contain;
        }

        .layui-table-cell {
            height: auto;
        }

        .fxProduct-container {
            padding: 20px;
        }

        .fxProduct-container {
            background-color: #fff;
        }
        .fxProduct-container form {
            width: 800px;
            margin: 0 auto;
            padding: 30px;
            border: 1px solid #eee;
        } 

        #inputContainer {
            margin-bottom: 20px;
            /* border: 1px solid #eee;   */
            padding: 0 40px
        }
 
        .layui-form-label {
            width: 140px;
        }
        .layui-input-block {
            margin-left: 170px;
        }
        #inputContainer .layui-input-block  {
            margin: 0;
            padding: 10px;
            width: 19%;
        }
        #inputContainer li {
            display: flex;
            width: 100%;
            border-bottom: 1px solid #eee;
            align-items: center;
        }
        #inputContainer .layui-textarea {
            height: 38px !important;
        }
        #inputContainer .layui-input,  #inputContainer .layui-textarea {
            min-height: auto;
            border-radius: 5px;
            text-align: center;
        }
        .item .layui-form-label{
            text-align: center;
            border-bottom: 1px solid #eee;
            width: 122px;
        }
    </style>
</head>

<body>
    <div class="productFxList-container">

        <!-- 搜索 -->
        <!-- <div class="searchInput">
            <form class="layui-form" action="" lay-filter="resets">
                <div class="layui-inline">
                    <label class="layui-form-label">商品名称：</label>
                    <input class="layui-input" name="distributionName" placeholder="请输入客户昵称" autocomplete="off">
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">创建日期：</label>
                    <input type="text" class="layui-input" id="test1" name="createTime" placeholder="请输入申请时间"
                        autocomplete="off">
                </div>
                <button class="layui-btn" data-type="reload" lay-submit="" lay-filter="searchForm">搜索</button>
                <button class="layui-btn layui-btn-primary" data-type="reload" lay-submit=""
                    lay-filter="reset">重置</button>
            </form>
        </div> -->


        <table lay-filter="productFxListTable" class="layui-hide" id="productFxListTable"></table>
    </div>

</body>

<!-- 添加 -->
<script type="text/html" id="toolbarDemo">
    <div class="bar">
        <div class="layui-btn-container" style="margin-bottom:-5px;">
            <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add">添加</button>
            <!-- <button class="layui-btn layui-btn-sm" lay-event="success">启用</button>
            <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="fail">停用</button> -->
        </div>

        <!-- 筛选 -->
        <!-- <div class="layui-form-item" id="status" style="margin-bottom:0">
            <label class="layui-form-label" style="width: 60px; text-align:right; margin-top:10px">状态：</label>
            <div class="layui-input-block" style="margin-left: 70px">
              <select name="stop" lay-filter="select">
                <option value="">全部</option>
                <option value="1">启用</option>
                <option value="0">停用</option>
              </select>
            </div>
        </div> -->
    </div>
</script>

<!-- 操作 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.all.min.js"></script>
<script src="../utils.js"></script>
<script src="../addProduct/layedit.js"></script>
<script>
    layui.use(['table', 'upload', 'form', 'jquery', "laydate"], function () {
        var table = layui.table,
            upload = layui.upload,
            form = layui.form,
            laydate = layui.laydate,
            $ = layui.jquery,
            layedit = layui.layedit;
            var editContext = "";

            table.render({
            elem: '#productFxListTable'
            , toolbar: "#toolbarDemo"
            , url: window.baseUrl + '/distribution/getAchievementConfigList'
            , method: 'post'
            , contentType: 'application/json'
            , headers: {
                Authorization: localStorage.token
            }
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            }
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": 200,
                    "count": res.total, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
            , request: {
                pageName: 'start' //页码的参数名称，默认：page
                , limitName: 'length' //每页数据量的参数名，默认：limit
            }
            , cols: [[
                { type: 'checkbox' }
                , { field: 'id', title: 'ID', width: 60, sort: true }
                , { field: 'configName', title: '业绩奖配置名称'}
                , { field: 'achievementLimit', title: '业绩总额限制', width: 140  }
                , { field: 'generations', title: '累积代数', width: 140  }
                , { field: 'levelFlag',  width: 140, title: '分销级别', templet: function (res) {
                        return res.levelFlag == "ZHONG_JI" ? '中级分销商': res.levelFlag == "GAO_JI" ? '高级分销商' : '-'
                    }}
                , { field: 'des', title: '会员荣誉身份描述', templet: function (res) {
                        return res.des || '-'
                    } }
                , {
                    title: '详情', width: 300, templet: function (res) {
                        // var str = ""
                        // for (let i = 0; i < res.configDetailList.length; i++) {
                        //     const s = res.configDetailList[i];
                        //     str += `
                        //         <ul class="layui-card" style="padding-left: 10px; border: 1px solid #eee;">
                        //             <li>ID：${s.id}</li>
                        //             <li>代数：${s.generations}</li>
                        //             <li>中级奖金：${s.zhongJiBonus}</li>
                        //             <li>高级奖金：${s.gaoJiBonus}</li>
                        //             <li>描述：${s.des || '-'}</li>
                        //         </ul>
                        //     `
                        // }

                        return "<span style='color: #2440B3; ' class='lookDetails' data-d='"+ JSON.stringify(res) +"'>查看详情</span>";
                    }
                }
                , {
                    title: '状态', width: 70, templet: function (res) {
                        return res.status == 1 ? '<span class="layui-badge layui-bg-orange">停用</span>': '<span class="layui-badge layui-bg-green">正常</span>'
                    }
                }
                , { field: "createTime", title: '添加时间', width: 180 }
                , { title: '操作',  width: 70, toolbar: '#barDemo' }
            ]],
            page: false,
            done: function() {
                $(".lookDetails").on("click", function() {
                    let data = $(this).attr("data-d");
                    showLayer(JSON.parse(data));
                    form.val('fxProduct', JSON.parse(data));
                    $("input, textarea, select").attr("disabled", true)
                    $(".delContainer, #addPushInput, button").hide();
                })
            }
        });

        laydate.render({
            elem: '#test1'
        });

        function showLayer(data) {
            layer.open({
                type: 1,
                title: "添加/编辑奖励配置",
                area: ['960px', '830px'], //宽高
                content: `
                    <div class="fxProduct-container">
                        <form class="layui-form" lay-filter="fxProduct">

                            <div class="layui-form-item">
                                <label class="layui-form-label"><span style="color:red; font-size:16px;">* </span>业绩奖配置名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="configName" lay-verify="required" lay-reqtext="业绩奖配置名称是必填项，岂能为空？" placeholder="请输入业绩奖配置名称"
                                        autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label"><span style="color:red; font-size:16px;">* </span>业绩总额限制</label>
                                <div class="layui-input-block">
                                    <input type="text" name="achievementLimit" lay-verify="required|number" lay-reqtext="业绩总额限制是必填项，岂能为空？" placeholder="请输入业绩总额限制"
                                        autocomplete="off" class="layui-input">
                                </div>
                            </div>
                             <div class="layui-form-item">
                                <label class="layui-form-label"><span style="color:red; font-size:16px;">* </span>业绩累积代数</label>
                                <div class="layui-input-block">
                                    <input type="text" name="generations" lay-verify="required|number" lay-reqtext="业绩累积代数是必填项，岂能为空？" placeholder="请输入业绩累积代数"
                                        autocomplete="off" class="layui-input">
                                </div>
                            </div>


                            <div class="layui-form-item">
                                <label class="layui-form-label">分销级别</label>
                                <div class="layui-input-inline">
                                    <select name="levelFlag" lay-verify="required" lay-search="">
                                        <option value="">直接选择或搜索选择</option>
                                        <option value="ZHONG_JI">中级分销商</option>
                                        <option value="GAO_JI">高级分销商</option>
                                    </select>
                                </div>
                            </div>


                            <div class="layui-form-item layui-form-text">
                                <label class="layui-form-label">会员荣誉身份描述</label>
                                <div class="layui-input-block">
                                    <textarea placeholder="请输入内容" name="des" class="layui-textarea"></textarea>
                                </div>
                            </div>

                            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
                                <legend>配置详情</legend>
                            </fieldset>

                            <div style="text-align:right; color: #2440b3; padding: 10px; cursor:pointer" id="addPushInput">添加配置详情</div>
                            <ul class="item">
                                <li style="margin-left: 40px;">
                                    <label class="layui-form-label"><span style="color:red; font-size:16px;">* </span>代数</label>
                                    <label class="layui-form-label"><span style="color:red; font-size:16px;">* </span>中级奖金</label>
                                    <label class="layui-form-label"><span style="color:red; font-size:16px;">* </span>高级奖金</label>
                                    <label class="layui-form-label">配置详情描述</label>
                                    <label class="layui-form-label">操作</label>
                                </li>
                                <ul id="inputContainer">
                                    <li>
                                        <div class="layui-input-block">
                                            <input type="text" name="mdzz" lay-verify="required|number" lay-reqtext="代数是必填项，岂能为空？" placeholder="请输入代数"
                                                autocomplete="off" class="layui-input">
                                        </div>
                                        <div class="layui-input-block">
                                            <input type="text" name="zhongJiBonus" lay-verify="required|number" lay-reqtext="中级奖金是必填项，岂能为空？" placeholder="请输入中级奖金" autocomplete="off" class="layui-input">
                                        </div>

                                        <div class="layui-input-block">
                                            <input type="text" name="gaoJiBonus" lay-verify="required|number" lay-reqtext="高级奖金是必填项，岂能为空？" placeholder="请输入高级奖金" autocomplete="off" class="layui-input">
                                        </div>

                                        <div class="layui-input-block">
                                            <textarea placeholder="请输入配置描述" name="childDes" class="layui-textarea"></textarea>
                                        </div>
                                        <div style="text-align: right; cursor: pointer; width: 60px; margin-left:16px">
                                            <span class="layui-badge layui-bg-orange delContainer">删除</span>
                                        </div>
                                    </li> 
                                </ul> 
                            </ul>


                            <input type="hidden" name="id" id="fxId" />

                            <div class="layui-form-item">
                                <label class="layui-form-label">状态</label>
                                <div class="layui-input-block" id="LAY-switch-check">
                                    <input type="checkbox" checked name="status" lay-skin="switch" lay-filter="switchTest" lay-text="启用|停用">
                                </div>
                            </div>
                    
                            
                            <div class="layui-form-item" style="text-align:center">
                                <div class="layui-input-block" style="margin-left: 0;">
                                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="addfxProduct">立即提交</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                </div>
                            </div>
                        </form>
                    </div>`,
                    success (layero, index){

                        form.render();

                        console.log(data, "---- data ----");
                        if (data) {
                            if (data.status == 0) {
                                $("#LAY-switch-check input").attr("checked", true)
                            } else {
                                $("#LAY-switch-check input").removeAttr("checked")
                            }
                            form.render()
                            
                            $("#inputContainer").empty();
                            data.configDetailList.forEach(el => {
                                $("#inputContainer").append(`
                                    <li>
                                        <input type="hidden" value="${el.id}" />
                                            <div class="layui-input-block">
                                                <input type="text" name="mdzz" lay-verify="required|number" value="${el.generations}" lay-reqtext="代数是必填项，岂能为空？" placeholder="请输入代数"
                                                    autocomplete="off" class="layui-input">
                                            </div>

                                            <div class="layui-input-block">
                                                <input type="text" name="zhongJiBonus" lay-verify="required|number" value="${el.zhongJiBonus}" lay-reqtext="中级奖金是必填项，岂能为空？" placeholder="请输入中级奖金"
                                                    autocomplete="off" class="layui-input">
                                            </div>

                                            <div class="layui-input-block">
                                                <input type="text" name="gaoJiBonus" lay-verify="required|number" value="${el.gaoJiBonus}" lay-reqtext="高级奖金是必填项，岂能为空？" placeholder="请输入高级奖金"
                                                    autocomplete="off" class="layui-input">
                                            </div>

                                            <div class="layui-input-block">
                                                <textarea value="${el.des}" placeholder="请输入配置描述" name="childDes" class="layui-textarea">${el.des || ''}</textarea>
                                            </div>
                                            <div style="text-align: right; cursor: pointer; width: 60px; margin-left:16px">
                                                <span class="layui-badge layui-bg-orange delContainer">删除</span>
                                            </div>
                                    </li>

                                `)
                                form.render();
  
                            })
                            
                        }
                        
                       // 添加
                       $("#addPushInput").on("click", function() {
                            let gaoJiBonus = $("#inputContainer").find("input[name='gaoJiBonus']");
                            let zhongJiBonus = $("#inputContainer").find("input[name='zhongJiBonus']");
                            let generations = $("#inputContainer").find("input[name='mdzz']");

                            for (let i = 0; i < gaoJiBonus.length; i++) {
                                let dom = gaoJiBonus[i];
                                if (!$(dom).val()) {
                                    layer.msg("请填写高级奖金后在进行添加选项")
                                    return false;
                                }
                            }

                            for (let i = 0; i < zhongJiBonus.length; i++) {
                                let dom = zhongJiBonus[i];
                                if (!$(dom).val()) {
                                    layer.msg("请填写中级奖金后在进行添加选项")
                                    return false;
                                }
                            }

                            for (let i = 0; i < generations.length; i++) {
                                let dom = generations[i];
                                if (!$(dom).val()) {
                                    layer.msg("请填写“代数”输入框后，在进行添加选项")
                                    return false;
                                }
                            }


                            $("#inputContainer").append(`
                                <li>
                                    <div class="layui-input-block">
                                            <input type="text" name="mdzz" lay-verify="required|number" lay-reqtext="代数是必填项，岂能为空？" placeholder="请输入代数"
                                                autocomplete="off" class="layui-input">
                                        </div>

                                        <div class="layui-input-block">
                                            <input type="text" name="zhongJiBonus" lay-verify="required|number" lay-reqtext="中级奖金是必填项，岂能为空？" placeholder="请输入中级奖金"
                                                autocomplete="off" class="layui-input">
                                        </div>

                                        <div class="layui-input-block">
                                            <input type="text" name="gaoJiBonus" lay-verify="required|number" lay-reqtext="高级奖金是必填项，岂能为空？" placeholder="请输入高级奖金"
                                                autocomplete="off" class="layui-input">
                                        </div>

                                        <div class="layui-input-block">
                                            <textarea placeholder="请输入配置描述" name="childDes" class="layui-textarea"></textarea>
                                        </div>
                                    <div style="text-align: right; cursor: pointer; width: 60px; margin-left:16px">
                                        <span class="layui-badge layui-bg-orange delContainer">删除</span>
                                    </div>
                                </li>

                            `)

                            form.render();

                             // 删除
                            $(".delContainer").on("click", function() {
                                if ($("#inputContainer .delContainer").length > 1) {
                                    $(this).parent().parent().remove();
                                }
                            })
                       })

                       // 删除
                       $(".delContainer").on("click", function() {
                            if ($("#inputContainer .delContainer").length > 1) {
                                $(this).parent().parent().remove();
                            }
                        })
                    }
            });
        }
        

        //工具栏事件
        table.on('toolbar(productFxListTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'success':
                    //页面层
                    var checkStatus = table.checkStatus('productFxListTable')
                        , data = checkStatus.data;
                    if (!data.length) {
                        return layer.msg("请选择！")
                    }
                    data.forEach(el => {
                        el.status = 1;
                        ajax({
                            path: "/distribution/saveOrUpdateAchievementConfig",
                            data: el,
                            success: function (res) {
                                table.reload("productFxListTable");
                            }
                        }, $)
                    })

                    layer.msg("更新成功！")
                    break;
                case 'fail':
                    var checkStatus = table.checkStatus('productFxListTable')
                        , data = checkStatus.data;
                    if (!data.length) {
                        return layer.msg("请选择！")
                    }
                    data.forEach(el => {
                        el.status = 0;
                        ajax({
                            path: "/distribution/saveOrUpdateAchievementConfig",
                            data: el,
                            success: function (res) {
                                table.reload("productFxListTable");
                            }
                        }, $)
                    })

                    layer.msg("更新成功！")
                    break;
                case "add":
                    showLayer();
                    $('#fxId').val("");
                    
                    // window.location.href = "../addProduct/addFxProduct.html";
                    break;
            };
        });

        // // 搜索
        // form.on("submit(searchForm)", function (e) {
        //     console.log(e);
        //     e.field.createTime = new Date(e.field.createTime).getTime();
        //     table.reload('productFxListTable', {
        //         page: {
        //             start: 1 //重新从第 1 页开始
        //         }
        //         , where: e.field
        //     });

        //     return false;
        // })


        // // 搜索
        // form.on("submit(reset)", function (e) {

        //     table.reload('productFxListTable', {
        //         page: {
        //             start: 1 //重新从第 1 页开始
        //         }
        //         , where: null
        //     });

        //     form.val("resets", {
        //         distributionName: "",
        //         createTime: ""
        //     });

        //     return false;
        // })


        // 筛选状态
        form.on("select(select)", function (e) {
            table.reload('productFxListTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                }
                , where: {
                    status: e.value,
                    goodsType: 0
                }
            });

        })


        // 编辑和删除
        table.on("tool(productFxListTable)", function (data) {
            switch (data.event) {
                case "edit":

                    showLayer(data.data);
                    data.data.status = data.data.status == 0 ?  true : false
                    form.val('fxProduct', data.data);
                    break;
            }
        })

        // 提交
        form.on("submit(addfxProduct)", function(obj) {
            obj.field.status = obj.field.status ? 0 : 1;


            let gaoJiBonus = $("#inputContainer").find("input[name='gaoJiBonus']");
            let zhongJiBonus = $("#inputContainer").find("input[name='zhongJiBonus']");
            let generations = $("#inputContainer").find("input[name='mdzz']");
            let childDes = $("#inputContainer").find("textarea[name='childDes']");

            var arr = [];

            for (let i = 0; i < gaoJiBonus.length; i++) {
                let dom = gaoJiBonus[i];
                if (!$(dom).val()) {
                    layer.msg("请填写高级奖金")
                    return false;
                } else {
                    arr[i] = {
                        gaoJiBonus: $(dom).val()
                    }
                }
            }

            for (let i = 0; i < zhongJiBonus.length; i++) {
                let dom = zhongJiBonus[i];
                if (!$(dom).val()) {
                    layer.msg("请填写中级奖金")
                    return false;
                } else {
                    arr[i].zhongJiBonus = $(dom).val()
                }
            }

            for (let i = 0; i < generations.length; i++) {
                let dom = generations[i];
                if (!$(dom).val()) {
                    layer.msg("请填写“代数”")
                    return false;
                } else {
                    arr[i].generations = $(dom).val()
                }
            }

            for (let i = 0; i < childDes.length; i++) {
                let dom = childDes[i];
                arr[i].des = $(dom).val()
            }

            obj.field.configDetailList = arr;
            
            console.log(obj.field);

            // 编辑
            if (obj.field.id) {
                ajax({
                    path: "/distribution/saveOrUpdateAchievementConfig",
                    data: obj.field,
                    success: function (res) {
                        layer.closeAll();
                        layer.msg("编辑成功!")
                        table.reload("productFxListTable");
                    }
                }, $)
            } else {
                delete obj.field.id;

                ajax({
                    path: "/distribution/saveOrUpdateAchievementConfig",
                    data: obj.field,
                    success: function (res) {
                        layer.closeAll();
                        layer.msg("添加成功!")
                        table.reload("productFxListTable");
                    }
                }, $)
            }

            return false;
        })
    });
</script>

</html>